package com.apes.framework.plugin.form.parser.matcher;

import com.apes.framework.plugin.log.timer.Timer;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * 功能：条件表达式模式匹配器
 *
 * @author xul
 * @create 2018-04-19 14:52
 */
public class ConditionalMatcher implements Matcher<String, List> {

    private Matcher parent;

    /**
     * 功能：获取识别模式
     */
    @Override
    public String getRegex() {
        return "(\\{)(.+?)(\\})";
    }

    @Override
    public Matcher getParent() {
        return parent;
    }

    @Override
    public void setParent(Matcher parent) {
        this.parent = parent;
    }

    /**
     * 功能：模式配置
     */
    @Override
//    @Timer("解析条件表达式")
    public List parse(String content) {
        List expressions = new ArrayList<>();
        Pattern pattern = Pattern.compile(getRegex());
        java.util.regex.Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            expressions.add(matcher.group(2).trim());
        }
        return expressions;
    }
}
